﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Particle_Tackle;
using SkinnedModel;

namespace SkinningSample
{
    class Player :　ObjectBase
    {
        private Model model;
        private AnimationPlayer animationPlayer;

        public Model PlayerModel {
            get { return this.model; }
            set 
            { 
                this.model = value;
                SkinningData skinningData = model.Tag as SkinningData;

                if (skinningData == null)
                    throw new InvalidOperationException
                        ("This model does not contain a SkinningData tag.");

                animationPlayer = new AnimationPlayer(skinningData);

                AnimationClip clip = skinningData.AnimationClips["Take 001"];

                animationPlayer.StartClip(clip);
            }
        }

        public override void Update(GameTime gameTime)
        {
            //スケール
            this.Scale += GameController.getLeftTriggerButton() / 100.0f;
            this.Scale -= GameController.getRightTriggerButton() / 100.0f;

            //回転
            this.Rotation += MathHelper.ToRadians(-GameController.getRightStickParamX());

            //移動
            this.Position = new Vector3(this.Position.X + (GameController.getLeftStickParamX() * (float)Math.Cos(this.Rotation) + GameController.getLeftStickParamY() * -(float)Math.Sin(this.Rotation)), 0.0f, this.Position.Z - (GameController.getLeftStickParamX() * (float)Math.Sin(this.Rotation) + GameController.getLeftStickParamY() * (float)Math.Cos(this.Rotation)));
            //this.Position.X += (GameController.getLeftStickParamX() * (float)Math.Cos(this.Rotation) + GameController.getLeftStickParamY() * -(float)Math.Sin(this.Rotation));
            //this.Position.Z -= (GameController.getLeftStickParamX() * (float)Math.Sin(this.Rotation) + GameController.getLeftStickParamY() * (float)Math.Cos(this.Rotation));

            animationPlayer.Update(gameTime.ElapsedGameTime, true, Matrix.Identity);
        }

        public override void Draw(GameTime gameTime, Matrix view, Matrix projection)
        {
            Matrix[] bones = animationPlayer.GetSkinTransforms();

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (SkinnedEffect effect in mesh.Effects)
                {
                    effect.SetBoneTransforms(bones);

                    effect.World = Matrix.CreateScale(this.Scale) * Matrix.CreateRotationY(this.Rotation) * Matrix.CreateTranslation(this.Position);
                    effect.View = view;
                    effect.Projection = projection;

                    // デフォルトのスキンメッシュのテクスチャをテスト用テクスチャに張り替え
                    //effect.Parameters["Texture"].SetValue(testTexture);

                    effect.EnableDefaultLighting();

                    effect.SpecularColor = new Vector3(0.25f);
                    effect.SpecularPower = 16;
                }

                mesh.Draw();
            }            

        }
    }
}
